package com.ddwl.user.controller;


import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.LocationBo;
import com.ddwl.schema.bo.user.DeviceBo;
import com.ddwl.schema.bo.user.DeviceSearchBo;
import com.ddwl.schema.bo.user.DeviceShareBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.user.DeviceVo;
import com.ddwl.user.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @date 2017/2/5
 */
@Auth(product = {BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.IOT})
@Api(tags = "设备管理服务")
@RequestMapping("/device")
@RestController
@RequiredArgsConstructor
public class DeviceController extends BaseController {

    private final DeviceService deviceService;

    @PostMapping
    @ApiOperation("我的蓝牙设备")
    public RestVo<List<DeviceVo>> user() {
        return RestVo.ok(deviceService.user(getUserId(), getPlatform()));
    }

    @PostMapping("near")
    @ApiOperation("附近的设备")
    public RestVo<List<DeviceVo>> near(@Validated @RequestBody LocationBo locationBo) {
        DeviceSearchBo param = new DeviceSearchBo();
        param.setUserId(getUserId());
        param.setLoc(locationBo);
        param.setPlatform(getPlatform());
        return RestVo.ok(deviceService.near(param));
    }

    @PostMapping("/save")
    @ApiOperation("保存蓝牙设备")
    public RestVo create(@Valid @RequestBody DeviceBo bo) {
        bo.setUserId(getUserId());
        bo.setPlatform(getPlatform());
        deviceService.save(bo);
        return RestVo.ok();
    }

    @PostMapping("/select")
    @ApiOperation("选择蓝牙设备")
    public RestVo updateSelected(@RequestBody DeviceBo bo) {
        deviceService.updateSelected(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备")
    public RestVo delete(@RequestParam String mac) {
        deviceService.delete(getUserId(), mac);
        return RestVo.ok();
    }

    @PostMapping("macs")
    @ApiOperation("我的设备Mac")
    public RestVo<List<String>> getMacs() {
        return RestVo.ok(deviceService.getMacs(getUserId(), getPlatform()));
    }

    @PostMapping("share")
    @ApiOperation("共享设备")
    public RestVo<Boolean> share(@RequestBody DeviceShareBo bo) {
        bo.setUserId(getUserId());
        deviceService.share(bo);
        return RestVo.ok();
    }

    @PostMapping("/conn")
    @ApiOperation("连接")
    public RestVo conn(@RequestBody DeviceBo bo) {
        bo.setUserId(getUserId());
        deviceService.conn(bo);
        return RestVo.ok();
    }


    @PostMapping("/connected")
    @ApiOperation("连接成功")
    public RestVo connected(@RequestBody DeviceBo bo) {
        bo.setUserId(getUserId());
        deviceService.connected(bo);
        return RestVo.ok();
    }

    @PostMapping("/disconnect")
    @ApiOperation("断开连接")
    public RestVo disconnect(@RequestBody DeviceBo bo) {
        bo.setUserId(getUserId());
        deviceService.disconnect(bo);
        return RestVo.ok();
    }

    @PostMapping("/online")
    @ApiOperation("在线设备")
    @Auth(required = false)
    public RestVo<List<DeviceVo>> online() {
        return RestVo.ok(deviceService.online());
    }

    @PostMapping("/info")
    @ApiOperation("设备信息")
    public RestVo<DeviceVo> info(@RequestBody DeviceBo bo) {
        bo.setUserId(getUserId());
        return RestVo.ok(deviceService.info(bo));
    }


}
